package com.example.demo.controller;
import com.example.demo.entity.Employee;
import com.example.demo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/employee")
@CrossOrigin(origins = "*")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/list")
    public List<Employee> list() {
        return employeeService.list();
    }

    // 添加登录接口
    // EmployeeController.java
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Employee employee) {
        Map<String, Object> result = new HashMap<>();
        Employee loginEmployee = employeeService.login(employee.getId(), employee.getPassword());
        if (loginEmployee != null) {
            result.put("code", 200);
            result.put("message", "登录成功");
            result.put("token", "1"); // 实际应用中需要生成有效的 token
        } else {
            result.put("code", 401);
            result.put("message", "账号或密码错误");
        }

        System.out.println("Login response: " + result); // 添加日志输出
        return result;
    }
}